part of '../store.dart';

Future<void> _handleMigrationFromFirstVersionToSecondOne({
  required FlutterSecureStorage secureStorage,
  required String userContentString,
}) async {
  final userContentJson = jsonDecode(userContentString) as Map<String, dynamic>;
  final userContentVersion = userContentJson['version'] as int?;

  if (userContentVersion != null) {
    var migratedUserContent = userContentJson.build();
    var migratedUserContentVersion = userContentVersion;

    if (migratedUserContentVersion == 1) {
      migratedUserContent = _migrateUserContentFromFirstVersionToSecondOne(
        userContent: migratedUserContent,
      );

      migratedUserContentVersion = 2;
    }

    final premiumAccessInfoContentJson = migratedUserContent['premium_access_info'] as Map<String, dynamic>?;

    String? premiumAccessInfoContent;

    if (premiumAccessInfoContentJson != null) {
      premiumAccessInfoContent = jsonEncode(premiumAccessInfoContentJson);
    }

    await Future.wait([
      secureStorage.write(
        key: 'user.premium_access_info',
        value: premiumAccessInfoContent,
      ),
      secureStorage.delete(
        key: 'user_info',
      ),
    ]);
  }
}

BuiltMap<String, dynamic> _migrateUserContentFromFirstVersionToSecondOne({
  required BuiltMap<String, dynamic> userContent,
}) {
  String parseTimeIso8601String({
    required Map<String, dynamic> timeJson,
  }) {
    final milliseconds = timeJson['ms'] as int;

    final time = DateTime.fromMillisecondsSinceEpoch(
      milliseconds,
      isUtc: true,
    );

    return time.toIso8601String();
  }

  final updatedUserContent = userContent.rebuild((userContent) {
    final demoExpirationTimeMap = userContent.remove('demo_expiration_time') as Map?;

    String? demoExpirationTimeString;

    if (demoExpirationTimeMap != null) {
      final demoExpirationTimeJson =
          Map.castFrom<dynamic, dynamic, String, dynamic>(demoExpirationTimeMap);

      demoExpirationTimeString = parseTimeIso8601String(
        timeJson: demoExpirationTimeJson,
      );
    }

    final premiumAccessInfoMap = userContent['premium_access_info'] as Map?;

    if (demoExpirationTimeString == null && premiumAccessInfoMap == null) {
      return;
    }

    final updatedPremiumAccessInfoJson = <String, dynamic>{};

    if (demoExpirationTimeString != null) {
      updatedPremiumAccessInfoJson['demo_expiration_time'] = demoExpirationTimeString;
    }

    if (premiumAccessInfoMap != null) {
      final premiumAccessInfoJson =
          Map.castFrom<dynamic, dynamic, String, dynamic>(premiumAccessInfoMap);

      var purchaseItemType = premiumAccessInfoJson['in_app_purchase_type'];

      if (purchaseItemType == 'lifetime_product') {
        purchaseItemType = 'lifetime_purchase_item';
      }

      if (purchaseItemType != null) {
        updatedPremiumAccessInfoJson['purchase_item_type'] = purchaseItemType;
      }

      final subscriptionExpirationTimeMap =
          premiumAccessInfoJson['subscription_expiration_time'] as Map?;

      String? subscriptionExpirationTimeString;

      if (subscriptionExpirationTimeMap != null) {
        final subscriptionExpirationTimeJson =
            Map.castFrom<dynamic, dynamic, String, dynamic>(subscriptionExpirationTimeMap);

        subscriptionExpirationTimeString = parseTimeIso8601String(
          timeJson: subscriptionExpirationTimeJson,
        );
      }

      if (subscriptionExpirationTimeString != null) {
        updatedPremiumAccessInfoJson['subscription_expiration_time'] =
            subscriptionExpirationTimeString;
      }
    }

    userContent['premium_access_info'] = updatedPremiumAccessInfoJson;
  });

  return updatedUserContent;
}
